package com.ThreadImp;
//多线程产生并发问题一个案列 龟兔赛跑
public class RaceThread implements Runnable{
    //静态字串
    private static String winner;

    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {

            //兔子跑10步休息一次
            if(Thread.currentThread().getName().equals("A") && i%10==0 ){
                try{
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            boolean fg = gameOver(i);
            if(fg){
                break;
            }

            System.out.println(Thread.currentThread().getName()+"跑了"+i+"步");
        }
    }
    //判断进度
    private boolean gameOver(int steps){
        if(winner!=null){
            return true;
        }
        if(steps >= 100){
            winner = Thread.currentThread().getName();
            System.out.println("胜利者->"+winner);
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        RaceThread raceThread = new RaceThread();
        //开启2个线程
        new Thread(raceThread,"A").start();
        new Thread(raceThread,"B").start();
    }
}
